#include "../GKoSonTools/BLUETOOTH/TI/TI.h" 
#include "../GKoSonTools/SYSCFG/sysCfg.h" 
#include "../GKoSonTools/GKUSART/gkusart.h"
#include "../GKoSonTools/UNIT/unit.h"

#include "../GKoSonTools/COMPONENT/KEILcomponent.h"
#include "../GKoSonTools/BLUETOOTH/TI/TIport.h" 
//框架状态机
enum{
INTERSIDE, //状态1
OUTERSIDE,  //状态2
RADIO,
RADIO2
};


static char U3STATUS=INTERSIDE;
static char Get_U3status(void){return U3STATUS;};
static void Set_U3status(char a){U3STATUS=a;};

//整个函数没有用途了 init的是WAKEUP拉低 就可以一直TXRX了
static uint8_t TI_cmd_mode(void)
{
  uint8_t count=0;
	HAL_GPIO_WritePin(BLERST_GPIO_Port, BLERST_Pin, GPIO_PIN_RESET);
	HAL_Delay(5);//不用再次开机
	HAL_GPIO_WritePin(BLERST_GPIO_Port, BLERST_Pin, GPIO_PIN_SET);

	HAL_GPIO_WritePin(BLECNT_GPIO_Port, BLECNT_Pin, GPIO_PIN_RESET);//拉低WAKEUP也就相当于CS脚 等待一会INT自动为低 可以U3收发啦
  HAL_Delay(40);
	while(HAL_GPIO_ReadPin(BLEINT_GPIO_Port,BLEINT_Pin))
	{//不用死等 延时即可
    HAL_Delay(40);
		printf("wakeup 0 waiting Int 0\r\n");
		count++;
		if(count>=10)
			return 0;
	}
	return 1;
}

static void TXRX_init(void)
{
	 GK_usart_init(&GKU3,3);
	 GKU3.Idle_callback=TI_Idle_RX_handle;
}


uint8_t BLE_TXRX(uint8_t *tx ,uint8_t txlen, uint8_t *rx ,uint16_t* rxlen)
{
	if(GK_usart_TXRX(&GKU3,tx,txlen,rx,rxlen))
	{
    All_printc(tx,txlen);
		printf("----");
		All_printc(rx,* rxlen);
		return 1;
	}
	else
	{
		printf("BLE_TXRX FAIL!!\r\n");
		return 0;
	}		
}


uint8_t BLE_shakehand(void)
{
		uint8_t TX[] = {"AT\r\n"};

		uint8_t backinfo[20] = {0x00};
		uint16_t len;
		if(BLE_TXRX(TX,sizeof(TX),backinfo,&len))
		{
			return 1;
		}
		return 0;

}
uint8_t BLE_readmac(void)
{
	uint8_t TX[] = {"AT+DEVID=?\r\n"};
//		uint8_t TX[] = {"AT+VERION=?\r\n"};//0100
//  	uint8_t TX[] = {"AT+SYS_INFO=?\r\n"};//中断来2次有bug//[BUFF小就死机]AT+Oole: S,V1.0,2019-1-21620,Role: S,V1.0,2019-1-216
		uint8_t backinfo[50] = {0x00};
		uint16_t len;
	  //All_printc(TX,sizeof(TX));
		if(BLE_TXRX(TX,sizeof(TX),backinfo,&len))
		{
			//printf("###");
		  //All_printc(backinfo,len);
			return 1;
		}
		return 0;

}
uint8_t BLE_rename(void)
{
	  uint8_t TX[] = {"AT+NAME=AKTIAABBCCDDEEFF\r\n"};
		uint8_t backinfo[20] = {0x00};
		uint16_t len;
		if(BLE_TXRX(TX,sizeof(TX),backinfo,&len))
		{
			//All_printc(backinfo,len);
			return 1;
		}
		return 0;

}

void TI_init(void)
{
	HAL_GPIO_WritePin(BLERST_GPIO_Port, BLERST_Pin, GPIO_PIN_SET);//高电平开机 文档：模组硬件复位引脚，低电平复位的意思是你拉低一下就复位 你要很快拉高！开机！
	Set_U3status(INTERSIDE);
	TXRX_init();//绑定U3 收发底层OK 它必须最后！因为前面串口会过来空白中断！
	HAL_GPIO_WritePin(BLECNT_GPIO_Port, BLECNT_Pin, GPIO_PIN_RESET);//拉低WAKEUP也就相当于CS脚 等待一会INT自动为低 可以U3收发啦
  HAL_Delay(40);
}
uint8_t TI_write_msg(uint8_t *ptSenddata , uint8_t uLegnth )
{
	HAL_UART_Transmit(&huart3, ptSenddata, uLegnth, 0xFFFF);
	return 1;
}

void TI_test(void)
{
	static char once=1;

	if(once)
	{
		 once=0;
	   TI_init();
		 printf("for once TI_init!\r\n");
	}
	if(Get_U3status()==INTERSIDE)
	{
		if(BLE_shakehand())
		{
		  Set_U3status(OUTERSIDE);
			HAL_Delay(500);
		}
		else
		{
			HAL_Delay(50);
			printf("fail");
		}
	}
	else if(Get_U3status()==OUTERSIDE)
	{
		//if(BLE_readmac())
		if(BLE_rename())//此后手机可以收到了:A T + C O N = S U C C E S S 
		{
			Set_U3status(RADIO);
			HAL_Delay(50);
		}
				else
		{
			HAL_Delay(50);
			printf("fail2");
		}
	}
	else if(Get_U3status()==RADIO)
	{
		if(BLE_readmac())
		{
			Set_U3status(RADIO2);
			HAL_Delay(50);
		}
				else
		{
			HAL_Delay(50);
			printf("fail3");
		}
	}
	else if(Get_U3status()==RADIO2)
	{
			HAL_Delay(500);
			//printf("free");
		  //TI_write_msg("TI",2);
		GKU3.status=1;
//		HAL_GPIO_WritePin(BLECNT_GPIO_Port, BLECNT_Pin, GPIO_PIN_RESET);
		if(ITFLAG)
			APP_handle(&GKU3);
		check_MAG_ok();
	}

}

